package es.xpt.ysf.commons.jaas.view;

import java.util.List;
import java.util.Locale;

import es.xpt.ysf.YSFException;
import es.xpt.ysf.commons.track.TrackController;
import es.xpt.ysf.jaas.YSFPrincipal;
import es.xpt.ysf.logging.LogManager;
import es.xpt.ysf.model.Item;
import es.xpt.ysf.track.TrackSearchParams;
import es.xpt.ysf.view.Controller;
import es.xpt.ysf.web.Constants;

@SuppressWarnings("serial")
public class UsersController extends Controller{ // NO_UCD
	
	private String entity;
	private String entityId;
	
	private List<YSFPrincipal> principalList;
	private YSFPrincipal principal = new YSFPrincipal(null);
	private List<Item> principalRoles;
	private String pass;
	private String repass;
	
	private UsersHelper helper = new UsersHelper();
	
	private TrackController trackController = new TrackController();	
	
	public void showTrackInfo() { 
		TrackSearchParams searchParams = new TrackSearchParams();
		searchParams.setAuthor(principal.getName());
		trackController.setOnlyResults(true);
		trackController.setSearchParams(searchParams);
		trackController.searchTracks();
		helper.setTrackInfoShown(true);
	}

	public void hideTrackInfo() { 
		helper.setTrackInfoShown(false);
	}
	
	public TrackController getTrackController() {
		return trackController;
	}

	public void setTrackController(TrackController trackController) {
		this.trackController = trackController;
	}

	public UsersController() {
		super();
		super.setMessageBundle("es.xpt.ysf.commons.jaas.Messages");
		principal = new YSFPrincipal(null);
	}
	
	public UsersController(String entity) {
		this();
		this.entity = entity;
	}
	
	public UsersController(String entity, String entityId) {
		this(entity);
		this.entityId = entityId;
	}
	
	
	public List<YSFPrincipal> getPrincipalList() {
		if ( principalList == null && entityId != null) {
			try {
				principalList = new JaasDelegate(getUserBean()).getEntityUsers(entity, entityId);
			} catch (YSFException e) {
				LogManager.getLog(getClass()).error(e.getMessage(), e);
			}
		}
		return principalList;
	}
	
	public String editUser() {
		try {
	           JaasDelegate d = new JaasDelegate(getUserBean());
	           principal = (YSFPrincipal) helper.getRow().getRowData();
	           helper.setEditMode(true);
	           principalRoles = d.getUserRoles(principal.getName());
	           return Constants.SUCCESS_OUTCOME.getValue();
	       } catch (YSFException e) {
	           LogManager.getLog(getClass()).error(e.getMessage(), e);
	           setErrorMessage(e);
	           return Constants.FAILURE_OUTCOME.getValue();
	       }
	}
	
	
	   public String addUser() {
	       try {
	           JaasDelegate d = new JaasDelegate(getUserBean());
	           d.insertUser(principal, pass, principalRoles, entityId, entity);
	           setInfoMessage("commons.user.create", principal.getName());
	           principalList = null;
	           principal = new YSFPrincipal(null);
	           principalRoles = null;
	           return Constants.SUCCESS_OUTCOME.getValue();
	       } catch (YSFException e) {
	           LogManager.getLog(getClass()).error(e.getMessage(), e);
	           setErrorMessage(e);
	           return Constants.FAILURE_OUTCOME.getValue();
	       }
	   }
	   
	   public String deleteUser() {
	       try {
	           JaasDelegate d = new JaasDelegate(getUserBean());
	           String name = ((YSFPrincipal) helper.getRow().getRowData()).getName();
	           d.deleteUser(entityId, entity, name);
	           principalList = null;
	           principal = new YSFPrincipal(null);
	           principalRoles = null;
	           setInfoMessage("commons.user.remove", name);
	           return Constants.SUCCESS_OUTCOME.getValue();
	       } catch (YSFException e) {
	           LogManager.getLog(getClass()).error(e.getMessage(), e);
	           setErrorMessage(e.getMessage());
	           return Constants.FAILURE_OUTCOME.getValue();
	       }
	   }
	   
	   public String updateUser() {
	       try {
	           JaasDelegate d = new JaasDelegate(getUserBean());
	           if ( pass!=null && pass.trim().length()>0 ) {
	        	   if ( repass==null || repass.trim().length()==0) {
	        		   setErrorMessage("");
	    	           return Constants.FAILURE_OUTCOME.getValue();
	        	   }
	        	   d.updateUser(principal, pass, principalRoles);
	           } else {
	        	   d.updateUser(principal, null, principalRoles);
	           }
	           setInfoMessage("commons.user.update", principal.getName());
	           principal = new YSFPrincipal(null);
	           principalRoles = null;
	           helper.setEditMode(false);
	           return Constants.SUCCESS_OUTCOME.getValue();
	       } catch (YSFException e) {
	           LogManager.getLog(getClass()).error(e.getMessage(), e);
	           setErrorMessage(e);
	           return Constants.FAILURE_OUTCOME.getValue();
	       }
	   }

	
	/**
	 * @return the entity
	 */
	public String getEntity() {
		return entity;
	}

	/**
	 * @param entity the entity to set
	 */
	public void setEntity(String entity) {
		this.entity = entity;
	}

	/**
	 * @return the entityId
	 */
	public String getEntityId() {
		return entityId;
	}

	/**
	 * @param entityId the entityId to set
	 */
	public void setEntityId(String entityId) {
		this.entityId = entityId;
	}

	/**
	 * @param principalList the principalList to set
	 */
	public void setPrincipalList(List<YSFPrincipal> principalList) {
		this.principalList = principalList;
	}



	/**
	 * @return the principal
	 */
	public YSFPrincipal getPrincipal() {
		return principal;
	}

	/**
	 * @param principal the principal to set
	 */
	public void setPrincipal(YSFPrincipal principal) {
		this.principal = principal;
	}
	
	public UsersHelper getHelper() {
		return helper;
	}

	public void setHelper(UsersHelper helper) {
		this.helper = helper;
	}

	/**
	 * @return the principalRoles
	 */
	public List<Item> getPrincipalRoles() {
		return principalRoles;
	}

	/**
	 * @param principalRoles the principalRoles to set
	 */
	public void setPrincipalRoles(List<Item> principalRoles) {
		this.principalRoles = principalRoles;
	}

	/**
	 * @return the pass
	 */
	public String getPass() {
		return pass;
	}

	/**
	 * @param pass the pass to set
	 */
	public void setPass(String pass) {
		this.pass = pass;
	}

	/**
	 * @return the repass
	 */
	public String getRepass() {
		return repass;
	}

	/**
	 * @param repass the repass to set
	 */
	public void setRepass(String repass) {
		this.repass = repass;
	}

	/**
	 * @return the principal locale string
	 */
	public String getLocale() {
		return principal != null && principal.getLocale() != null? principal.getLocale().toString(): "";
	}

	/**
	 * @param the locale string to set for the principal
	 */
	public void setLocale(String localeStr) {
		Locale locale;
		if (localeStr == null || (localeStr.length() != 2 && localeStr.length() != 5)) {
			locale = super.getUserBean().getLocale();
		} else {
			locale = localeStr.length() == 2?  new Locale(localeStr.toLowerCase()):
				new Locale(localeStr.substring(0, 2).toLowerCase(),localeStr.substring(3, 5).toUpperCase());
		}
		principal.setLocale(locale);
	}

}
